﻿using System.Windows.Forms;

using Pibby.Core;
using System.Drawing;
using System.Collections.Generic;

namespace Pibby.Controls
{
	public enum ListConfiguration
	{
		HideCheckBoxes,
		ShowCheckBoxes
	}

	public class EpisodeListView : SortableListView
	{
		public virtual void Configure(ListConfiguration configuration)
		{
			this.View = View.Details;
			this.CheckBoxes = configuration == ListConfiguration.ShowCheckBoxes;

			this.Columns.Add("Episode", 100, HorizontalAlignment.Left);
			this.Columns.Add("Duration", 100, HorizontalAlignment.Right);
			this.Columns.Add("Publication Date", 100, HorizontalAlignment.Left);

			this.RefreshColumnSpacing();

			this.FullRowSelect = true;
			this.MultiSelect = true;
		}

		public virtual void UpdateContent(EpisodeCollection collection)
		{
			List<ListViewItem> items = new List<ListViewItem>();

			foreach (Episode e in collection)
			{
				items.Add(new EpisodeListViewItem(e));
			}

			base.UpdateContent(items);
		}

		public virtual void RefreshContent(EpisodeCollection collection)
		{
			this.UpdateContent(collection);

			this.SortByColumn(2, SortOrder.Descending);
		}

		public virtual void RefreshColumnSpacing()
		{
			int columnWidth = this.ClientSize.Width / 10;

			if (this.Columns.Count == 3)
			{
				this.Columns[0].Width = columnWidth * 4;
				this.Columns[1].Width = columnWidth * 2;
				this.Columns[2].Width = columnWidth * 3;
			}
		}

		public void CycleView()
		{
			bool smallIcon = this.SmallImageList != null;
			bool largeIcon = this.LargeImageList != null;

			switch (this.View)
			{
				case View.LargeIcon:

					if (smallIcon)
						this.View = View.SmallIcon;
					else
						this.View = View.Details;

					break;

				case View.SmallIcon:

					this.View = View.Details;
					break;

				case View.Details:

					this.View = View.List;
					break;
					
				case View.List:

					if (largeIcon)
					{
						this.View = View.LargeIcon;
					}
					else
					{
						if (smallIcon)
							this.View = View.SmallIcon;
						else
							this.View = View.Details;
					}
					break;

				default:

					this.View = View.Details;
					break;
			}
		}


	}

	public class AgedEpisodeListView : EpisodeListView
	{
		public AgedEpisodeListView()
			: base()
		{
		}

		public override void Configure(ListConfiguration configuration)
		{
			this.View = View.Details;
			this.CheckBoxes = configuration == ListConfiguration.ShowCheckBoxes;

			this.Columns.Add("Episode", 100, HorizontalAlignment.Left);
			this.Columns.Add("Duration", 100, HorizontalAlignment.Right);
			this.Columns.Add("Publication Date", 100, HorizontalAlignment.Left);
            this.Columns.Add("Podcast", 100, HorizontalAlignment.Left);

			this.RefreshColumnSpacing();

			this.FullRowSelect = true;
			this.MultiSelect = true;

			this.MessageWhenEmpty = "Nothing to see here";
		}

		public override void UpdateContent(EpisodeCollection collection)
		{
			List<ListViewItem> items = new List<ListViewItem>();

			foreach (Episode e in collection)
			{
				items.Add(new AgedEpisodeListViewItem(e));
			}

			base.UpdateContent(items);
		}
	}
}

